package xcmg.device.service.account;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.account.ArapDO;
import xcmg.device.dao.entity.account.ArapDetailDO;
import xcmg.device.dao.entity.account.CheckDO;
import xcmg.device.dao.entity.account.CheckDetailDO;
import xcmg.device.dao.entity.account.RefundCheckDO;
import xcmg.device.dao.entity.agentReturn.AgentReturnDO;
import xcmg.device.dao.entity.company.CompanyDealerDO;
import xcmg.device.dao.entity.prepPay.PrepPayDO;
import xcmg.device.dao.entity.sale.SalesBillDO;
import xcmg.device.dao.entity.sale.SalesOutBillDO;
import xcmg.device.dao.mapper.CompanyDealerMapper;
import xcmg.device.dao.mapper.account.ArapDetailMapper;
import xcmg.device.dao.mapper.account.ArapMapper;
import xcmg.device.dao.mapper.account.CheckDetailMapper;
import xcmg.device.dao.mapper.account.CheckMapper;
import xcmg.device.dao.mapper.account.RefundCheckMapper;
import xcmg.device.dao.mapper.agentReturn.AgentReturnDOMapper;
import xcmg.device.dao.mapper.prepPay.PrepPayDOMapper;
import xcmg.device.dao.mapper.sale.SalesBillMapper;
import xcmg.device.dao.mapper.sale.SalesOutBillMapper;
import xcmg.device.infra.CalcUtil;
import xcmg.device.infra.DateUtil;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.ExportUtil;
import xcmg.device.infra.enums.BillDesc;
import xcmg.device.service.GenDocumentService;
import xcmg.device.service.agentReturn.AgentReturnService;
import xcmg.device.service.company.DealerVehicleService;
import xcmg.device.service.vo.account.RefundCheckParam;
import xcmg.device.service.vo.account.RefundCheckVO;
import xcmg.device.service.vo.agentReturn.AgentReturnShowVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;
import yb.ecp.fast.infra.infra.log.LogHelper;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by wrg on 2019/6/28.
 */
@Service
public class RefundCheckService {
    @Autowired
    private RefundCheckMapper refundCheckMapper;
    @Autowired
    private FastGenClient genClient;
    @Autowired
    private GenDocumentService genDocumentService;
    @Autowired
    private AgentReturnService agentReturnService;
    @Autowired
    private CheckMapper checkMapper;
    @Autowired
    private CheckDetailMapper checkDetailMapper;
    @Autowired
    private ArapDetailMapper arapDetailMapper;
    @Autowired
    private ArapMapper arapMapper;
    @Autowired
    private PrepPayDOMapper prepPayDOMapper;
    @Autowired
    private CompanyDealerMapper companyDealerMapper;
    @Autowired
    private SalesOutBillMapper salesOutBillMapper;
    @Autowired
    private SalesBillMapper salesBillMapper;
    @Autowired
    private DealerVehicleService dealerVehicleService;
    @Autowired
    private AgentReturnDOMapper agentReturnDOMapper;
    @Autowired
    private HttpServletResponse response;

    //获取列表
    public PageCommonVO list(SearchCommonVO<RefundCheckParam> condition) {
        PageCommonVO result = new PageCommonVO();
        PageHelperPlus.orderBy("create_date desc");
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        List<RefundCheckDO> doList = refundCheckMapper.list(condition.getFilters());
        List<RefundCheckVO> voList = new ArrayList<>();
        for (RefundCheckDO entity : doList) {
            RefundCheckVO model = new RefundCheckVO();
            BeanUtils.copyProperties(entity, model);
            voList.add(model);
        }
        result.setPageInfo(new PageInfo(doList));
        result.setPageInfoList(voList);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode add(RefundCheckVO addCheckVO) throws Exception {
        ActionResult<String> textResult = genClient.newGuidText();
        if (textResult.getCode() != ErrorCode.Success.getCode() || StringUtils.isBlank(textResult.getValue())) {
            return ErrorCode.IllegalArument;
        }
        RefundCheckDO record = new RefundCheckDO();
        BeanUtils.copyProperties(addCheckVO, record);
        record.setId(textResult.getValue());
        record.setRefundOrder(genDocumentService.getDocumentNo(BillDesc.FDRefundCheck));
        record.setCreateDate(new Date());

        AgentReturnShowVO agentReturnShowVO = agentReturnService.item(record.getRefundId());
        if (null == agentReturnShowVO) {
            throw new Exception("退货单信息不存在");
        }
        AgentReturnDO agentReturnDO = new AgentReturnDO();
        agentReturnDO.setId(agentReturnShowVO.getId());
        agentReturnDO.setCheckState(2);
        int updateCount = agentReturnDOMapper.updateByPrimaryKeySelective(agentReturnDO);
        if (updateCount <= 0) {
            throw new Exception("退货单结算状态修改失败");
        }

        //更新应收记录，预付款信息
        updateArapInfo(record, agentReturnShowVO);
        //修改预付款
        updatePrepay(record, agentReturnShowVO, addCheckVO.getOrgId());
        //更新车辆额度信息
        updateVehicleQuota(record, agentReturnShowVO);

        int refundCheckAddCount = refundCheckMapper.insertSelective(record);
        if (refundCheckAddCount <= 0) {
            throw new Exception("新增退款信息失败");
        }

        return ErrorCode.Success;
    }

    //修改预付款
    private void updatePrepay(RefundCheckDO record, AgentReturnShowVO agentReturnShowVO, String orgId) throws Exception {
        BigDecimal advancePayRefundPrice = record.getAdvancePayRefundPrice();
        if (null == advancePayRefundPrice || advancePayRefundPrice.compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }
        //增加预收款金额，同时插入结算卡信息
        String saleOutId = agentReturnShowVO.getSaleOutId();
        SalesOutBillDO salesOutBillDO = salesOutBillMapper.selectByPrimaryKey(saleOutId);
        if (null == salesOutBillDO) {
            throw new Exception("销售出库单信息不存在");
        }
        SalesBillDO salesBillDO = salesBillMapper.selectBySalesBillNo(salesOutBillDO.getSalesBillNo());
        if (null == salesBillDO) {
            throw new Exception("销售单信息不存在");
        }
        //生成应收记录
        CompanyDealerDO dealerDO = companyDealerMapper.getDealer(orgId, salesBillDO.getClientId());
        if (null == dealerDO) {
            throw new Exception("客户信息不存在");
        }
        ArapDO arapDO = arapMapper.selectByAccOrgId(orgId, dealerDO.getDealerOrgId(), 0);
        if (arapDO == null) {
            arapDO = new ArapDO();
            arapDO.setId(genClient.newGuidText().getValue());
            arapDO.setOrgId(orgId);
            arapDO.setAccOrgId(dealerDO.getDealerOrgId());
            arapDO.setAccOrgName(salesOutBillDO.getClientName());
            arapDO.setDealerCode(dealerDO.getDealerCode());
            arapDO.setArapType(0);
            arapDO.setPrepPayPrice(advancePayRefundPrice);
            arapDO.setCreateDate(new Date());
            int insert = arapMapper.insert(arapDO);
            if (insert <= 0) {
                throw new Exception("生成应收记录失败");
            }
        } else {
            arapDO.setOldPrepPayPrice(arapDO.getPrepPayPrice());
            arapDO.setPrepPayPrice(arapDO.getPrepPayPrice().add(advancePayRefundPrice));
            arapDO.setUpdateDate(new Date());
            int updateCount = arapMapper.updateSelective(arapDO);
            if (updateCount <= 0) {
                throw new Exception("生成应收记录失败");
            }
        }
        //新增结算卡信息
        addPrepPayInfo(record, dealerDO, arapDO, advancePayRefundPrice);
    }

    //更新应收信息，预付款信息
    private void updateArapInfo(RefundCheckDO record, AgentReturnShowVO agentReturnShowVO) throws Exception {
        BigDecimal deductibleAmount = record.getDeductibleAmount();
        //抵扣应收记录的应收金额
        if (null == deductibleAmount || deductibleAmount.compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }

        ArapDetailDO arapDetailDO = arapDetailMapper.selectByBillNo(record.getOrgId(), agentReturnShowVO.getSaleOutNo());
        if (null == arapDetailDO) {
            throw new Exception("应收记录详情不存在");
        }

        String checkId = genClient.newGuidText().getValue();
        if (StringUtils.isBlank(checkId)) {
            throw new Exception("收款记录主键生成失败");
        }

        addCheckDetail(agentReturnShowVO, deductibleAmount, arapDetailDO, checkId);
        addCheckInfo(record, checkId, deductibleAmount);
        updateArapDetail(deductibleAmount, arapDetailDO);
        updateArap(deductibleAmount, arapDetailDO);

    }

    //修改应收记录
    private void updateArap(BigDecimal deductibleAmount, ArapDetailDO arapDetailDO) throws Exception {
        ArapDO arapDO = arapMapper.selectByAccOrgId(arapDetailDO.getSupplierOrgId(), arapDetailDO.getPurchaseOrgId(), 0);
        if (null == arapDO) {
            throw new Exception("应收记录不存在");
        }
        ArapDO updateArapDO = new ArapDO();
        updateArapDO.setId(arapDO.getId());
        updateArapDO.setReceiverPrice(arapDO.getReceiverPrice().subtract(deductibleAmount));
        updateArapDO.setOldReceiverPrice(arapDO.getReceiverPrice());
        int updateArapCount = arapMapper.updateSelective(updateArapDO);
        if (updateArapCount <= 0) {
            throw new Exception("应收记录金额修改失败");
        }
    }

    //修改应收详情记录
    private void updateArapDetail(BigDecimal deductibleAmount, ArapDetailDO arapDetailDO) throws Exception {
        ArapDetailDO updateArapDetailDO = new ArapDetailDO();
        updateArapDetailDO.setId(arapDetailDO.getId());
        BigDecimal newRemainPrice = arapDetailDO.getRemainPrice().subtract(deductibleAmount);
        if (newRemainPrice.compareTo(BigDecimal.ZERO) < 0) {
            throw new Exception("抵扣欠款金额不能大于应收金额");
        }

        if (newRemainPrice.compareTo(BigDecimal.ZERO) == 0) {
            updateArapDetailDO.setIsSettle(1);
            SalesOutBillDO salesOutBillDO = salesOutBillMapper.selectBySalesOutBillNo(arapDetailDO.getBillNo());
            if (null == salesOutBillDO) {
                throw new Exception("销售出库单不存在");
            }
            SalesOutBillDO updateSalesOutBillDO = new SalesOutBillDO();
            updateSalesOutBillDO.setId(salesOutBillDO.getId());
            updateSalesOutBillDO.setCheckState((short) 2);
            int updateCount = salesOutBillMapper.updateByPrimaryKeySelective(updateSalesOutBillDO);
            if (updateCount <= 0) {
                throw new Exception("修改销售出库单结算状态失败");
            }
        }
        updateArapDetailDO.setRemainPrice(newRemainPrice);//应收
        updateArapDetailDO.setReceivePrice(arapDetailDO.getReceivePrice().add(deductibleAmount));//已收
        int updateCount = arapDetailMapper.update(updateArapDetailDO);
        if (updateCount <= 0) {
            throw new Exception("应收记录详情应收金额修改失败");
        }
    }

    //新增收款详情信息
    private void addCheckDetail(AgentReturnShowVO agentReturnShowVO, BigDecimal deductibleAmount, ArapDetailDO arapDetailDO, String checkId) throws Exception {
        CheckDetailDO detailDO = new CheckDetailDO();
        detailDO.setArapDetailId(arapDetailDO.getId());
        detailDO.setBillDate(agentReturnShowVO.getCreateTime());
        detailDO.setBillId(agentReturnShowVO.getSaleOutId());
        detailDO.setBillNo(agentReturnShowVO.getSaleOutNo());
        detailDO.setBillSingle(BillDesc.SalesOutNo.getDesc());
        detailDO.setRefundAmount(deductibleAmount);
        detailDO.setTotalPrice(deductibleAmount);
        detailDO.setReceivePrice(deductibleAmount);
        detailDO.setCheckPrice(deductibleAmount);
        detailDO.setMainId(checkId);
        detailDO.setSaleBillType(agentReturnShowVO.getSaleType());
        boolean success = checkDetailMapper.insert(detailDO) > 0;
        if (!success) {
            throw new Exception("操作失败");
        }
    }

    //新增收款信息
    private void addCheckInfo(RefundCheckDO record, String id, BigDecimal deductibleAmount) throws Exception {
        CheckDO checkDO = new CheckDO();
        checkDO.setId(id);
        checkDO.setCheckOrder(genDocumentService.getDocumentNo(BillDesc.FDCheck));
        checkDO.setCreateDate(new Date());
        checkDO.setCheckType(0);
        checkDO.setCreateDate(record.getCreateDate());
        checkDO.setCreateId(record.getCreateId());
        checkDO.setCreateName(record.getCreateName());
        checkDO.setOperatorId(record.getOperatorId());
        checkDO.setOperatorName(record.getOperatorName());
        checkDO.setSettleType(0);
        // checkDO.setTotalPrice(deductibleAmount);
        checkDO.setRefundAmount(deductibleAmount);
        checkDO.setRefundOrderId(record.getId());
        checkDO.setSupplierOrgId(record.getOrgId());
        boolean success = checkMapper.insert(checkDO) > 0;
        if (!success) {
            throw new Exception("新增收付款记录失败");
        }
        record.setCheckId(id);
    }

    //新增结算卡信息
    private void addPrepPayInfo(RefundCheckDO record, CompanyDealerDO dealerDO, ArapDO updateArapDO, BigDecimal advancePayRefundPrice) throws Exception {
        PrepPayDO prepPayDO = new PrepPayDO();
        prepPayDO.setId(genClient.newGuidText().getValue());
        prepPayDO.setBillType(2);
        prepPayDO.setCustomerName(dealerDO.getCompanyName());
        prepPayDO.setCustomerId(String.valueOf(dealerDO.getId()));
        prepPayDO.setTotalAmount(updateArapDO.getPrepPayPrice());
        prepPayDO.setTransactionAmount(advancePayRefundPrice);
        prepPayDO.setCreateId(record.getCreateId());
        prepPayDO.setOrgId(record.getOrgId());
        prepPayDO.setDiscardFlag(0);
        prepPayDO.setCreateDate(new Date());
        prepPayDO.setBillNo(record.getRefundOrder());
        int count = prepPayDOMapper.insertSelective(prepPayDO);
        if (count <= 0) {
            throw new Exception("新增预付款信息失败");
        }
    }

    //更新客户额度信息
    private void updateVehicleQuota(RefundCheckDO record, AgentReturnShowVO agentReturnShowVO) throws Exception {
        BigDecimal useVehicleRefundQuota = record.getUseVehicleRefundQuota();
        if (null == useVehicleRefundQuota || useVehicleRefundQuota.compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }
        //还回车辆额度
        String saleOutId = agentReturnShowVO.getSaleOutId();
        SalesOutBillDO salesOutBillDO = salesOutBillMapper.selectByPrimaryKey(saleOutId);
        //根据销售单更新客户车辆使用额度
        SalesBillDO salesBillDO = salesBillMapper.selectBySalesBillNo(salesOutBillDO.getSalesBillNo());
        if (null == salesBillDO) {
            throw new Exception("销售单信息不存在");
        }
        ErrorCode errorCode = dealerVehicleService.updateQuota(salesBillDO, CalcUtil.subtract(BigDecimal.ZERO, useVehicleRefundQuota), record.getOrgId());
        if (ErrorCode.Success != errorCode) {
            throw new Exception("客户车辆额度扣减失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode reverse(String id, String orgId, String userId) throws Exception {
        RefundCheckDO refundCheckDO = refundCheckMapper.selectByPrimaryKey(id);
        refundCheckDO.setIsReverse(1);
        int updateRefundCount = refundCheckMapper.updateByPrimaryKeySelective(refundCheckDO);
        if (updateRefundCount <= 0) {
            LogHelper.monitor("修改退款单据红冲状态失败");
            return ErrorCode.Failure;
        }

        AgentReturnShowVO agentReturnShowVO = agentReturnService.item(refundCheckDO.getRefundId());
        if (null == agentReturnShowVO) {
            throw new Exception("退货单信息不存在");
        }
        AgentReturnDO agentReturnDO = new AgentReturnDO();
        agentReturnDO.setId(agentReturnShowVO.getId());
        agentReturnDO.setCheckState(0);
        int updateCount = agentReturnDOMapper.updateByPrimaryKeySelective(agentReturnDO);
        if (updateCount <= 0) {
            throw new Exception("退货单结算状态修改失败");
        }

        //更新应收记录，预付款信息
        reverseUpdateArap(refundCheckDO, agentReturnShowVO);
        //回退预付款
        reverseUpdatePrepay(refundCheckDO, agentReturnShowVO, orgId,userId);
        //更新车辆额度信息
        BigDecimal useVehicleRefundQuota = refundCheckDO.getUseVehicleRefundQuota() == null ? BigDecimal.ZERO : CalcUtil.subtract(BigDecimal.ZERO, refundCheckDO.getUseVehicleRefundQuota());
        if (useVehicleRefundQuota.compareTo(BigDecimal.ZERO) != 0) {
            refundCheckDO.setUseVehicleRefundQuota(useVehicleRefundQuota);
            updateVehicleQuota(refundCheckDO, agentReturnShowVO);
        }

        return ErrorCode.Success;
    }

    //红冲预付款
    private void reverseUpdatePrepay(RefundCheckDO record, AgentReturnShowVO agentReturnShowVO, String orgId,String userId) throws Exception {
        BigDecimal advancePayRefundPrice = record.getAdvancePayRefundPrice();
        if (null == advancePayRefundPrice || advancePayRefundPrice.compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }
        //增加预收款金额，同时插入结算卡信息
        String saleOutId = agentReturnShowVO.getSaleOutId();
        SalesOutBillDO salesOutBillDO = salesOutBillMapper.selectByPrimaryKey(saleOutId);
        if (null == salesOutBillDO) {
            throw new Exception("销售出库单信息不存在");
        }

        SalesBillDO salesBillDO = salesBillMapper.selectBySalesBillNo(salesOutBillDO.getSalesBillNo());
        if (null == salesBillDO) {
            throw new Exception("销售单信息不存在");
        }

        //回退应收记录
        CompanyDealerDO dealerDO = companyDealerMapper.getDealer(orgId, salesBillDO.getClientId());
        if (null == dealerDO) {
            throw new Exception("客户信息不存在");
        }

        ArapDO arapDO = arapMapper.selectByAccOrgId(orgId, dealerDO.getDealerOrgId(), 0);
        if (arapDO == null) {
            throw new Exception("预付款信息不存在");
        }

        arapDO.setOldPrepPayPrice(arapDO.getPrepPayPrice());
        arapDO.setPrepPayPrice(arapDO.getPrepPayPrice().subtract(advancePayRefundPrice));
        arapDO.setUpdateDate(new Date());
        int updateCount = arapMapper.updateSelective(arapDO);
        if (updateCount <= 0) {
            throw new Exception("回退预付款信息失败");
        }
        //作废结算卡信息
        String refundOrder = record.getRefundOrder();
        reversePrepPayInfo(refundOrder, orgId, userId);
    }

    //红冲应收信息
    private void reverseUpdateArap(RefundCheckDO record, AgentReturnShowVO agentReturnShowVO) throws Exception {
        BigDecimal deductibleAmount = record.getDeductibleAmount();
        //抵扣应收记录的应收金额
        if (null == deductibleAmount || deductibleAmount.compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }

        ArapDetailDO arapDetailDO = arapDetailMapper.selectByBillNo(record.getOrgId(), agentReturnShowVO.getSaleOutNo());
        if (null == arapDetailDO) {
            throw new Exception("应收记录详情不存在");
        }

        reverseCheckInfo(record.getCheckId());
        reverseArapDetail(deductibleAmount, arapDetailDO);
        reverseArap(deductibleAmount, arapDetailDO);

    }

    //红冲应收信息
    private void reverseArap(BigDecimal deductibleAmount, ArapDetailDO arapDetailDO) throws Exception {
        ArapDO arapDO = arapMapper.selectByAccOrgId(arapDetailDO.getSupplierOrgId(), arapDetailDO.getPurchaseOrgId(), 0);
        if (null == arapDO) {
            throw new Exception("应收记录不存在");
        }
        ArapDO updateArapDO = new ArapDO();
        updateArapDO.setId(arapDO.getId());
        updateArapDO.setReceiverPrice(arapDO.getReceiverPrice().add(deductibleAmount));
        updateArapDO.setOldReceiverPrice(arapDO.getReceiverPrice());
        int updateArapCount = arapMapper.updateSelective(updateArapDO);
        if (updateArapCount <= 0) {
            throw new Exception("应收记录金额修改失败");
        }
    }

    //红冲应收详情信息
    private void reverseArapDetail(BigDecimal deductibleAmount, ArapDetailDO arapDetailDO) throws Exception {
        BigDecimal newReceivePrice = arapDetailDO.getReceivePrice().subtract(deductibleAmount);
        BigDecimal totalPrice = arapDetailDO.getTotalPrice();

        SalesOutBillDO salesOutBillDO = salesOutBillMapper.selectBySalesOutBillNo(arapDetailDO.getBillNo());
        if (null == salesOutBillDO) {
            throw new Exception("销售出库单不存在");
        }

        SalesOutBillDO updateSalesOutBillDO = new SalesOutBillDO();
        updateSalesOutBillDO.setId(salesOutBillDO.getId());
        if (totalPrice.compareTo(newReceivePrice) > 0) {
            updateSalesOutBillDO.setCheckState((short) 1);
        } else {
            updateSalesOutBillDO.setCheckState((short) 0);
        }
        int updateSaleOutCount = salesOutBillMapper.updateByPrimaryKeySelective(updateSalesOutBillDO);
        if (updateSaleOutCount <= 0) {
            throw new Exception("修改销售出库单结算状态失败");
        }

        ArapDetailDO updateArapDetailDO = new ArapDetailDO();
        updateArapDetailDO.setId(arapDetailDO.getId());
        updateArapDetailDO.setIsSettle(0);
        updateArapDetailDO.setReceivePrice(newReceivePrice);//已收
        updateArapDetailDO.setRemainPrice(arapDetailDO.getRemainPrice().add(deductibleAmount));//应收
        int updateCount = arapDetailMapper.update(updateArapDetailDO);
        if (updateCount <= 0) {
            throw new Exception("应收记录详情应收金额修改失败");
        }
    }

    //红冲收款信息
    private void reverseCheckInfo(String id) throws Exception {
        CheckDO checkDO = checkMapper.selectByPrimaryKey(id);
        if (null == checkDO) {
            throw new Exception("收付款记录信息不存在");
        }
        checkDO.setIsReverse(1);
        boolean success = checkMapper.update(checkDO) > 0;
        if (!success) {
            throw new Exception("修改收付款记录红冲状态失败");
        }
    }

    //红冲结算卡信息
    private void reversePrepPayInfo(String refundOrder, String orgId, String userId) throws Exception {
        PrepPayDO record = new PrepPayDO();
        record.setDiscardFlag(1);
        record.setDiscardDate(new Date());
        record.setDiscardPersonId(userId);
        record.setBillNo(refundOrder);
        record.setOrgId(orgId);
        int updateCount = prepPayDOMapper.updateByBillNo(record);
        if (updateCount <= 0) {
            throw new Exception("更新预付款记录失败");
        }
    }


    public void export(List<String> ids) throws Exception {
        PageHelperPlus.orderBy("create_date desc");
        invokeExport(refundCheckMapper.findByIds(ids));
    }

    public void exportAll(RefundCheckParam condition) throws Exception {
        PageHelperPlus.orderBy("create_date desc");
        List<RefundCheckDO> list = refundCheckMapper.list(condition);
        invokeExport(list);
    }

    private void invokeExport(List<RefundCheckDO> doList) throws Exception {
        String title = "退款单数据导出";
        String[] rowsName = new String[]{"序号", "退款单号", "结算方式", "单据总金额", "折损金额",
                "预付款退款金额", "使用车辆退款额度", "退款金额", "抵扣欠款金额", "经办人", "收款时间", "是否红冲"};
        List<Object[]> dataList = new ArrayList<>();
        Object[] objs;
        for (int i = 0; i < doList.size(); i++) {
            RefundCheckDO refundCheckDO = doList.get(i);
            objs = new Object[rowsName.length];
            objs[0] = i + 1;
            objs[1] = refundCheckDO.getRefundOrder();
            objs[2] = refundCheckDO.getSettleType() == 0 ? "按订单结算" : "按金额结算";
            objs[3] = refundCheckDO.getTotalPrice();
            objs[4] = refundCheckDO.getLossAmount();
            objs[5] = refundCheckDO.getAdvancePayRefundPrice();
            objs[6] = refundCheckDO.getUseVehicleRefundQuota();
            objs[7] = refundCheckDO.getRefundAmount();
            objs[8] = refundCheckDO.getDeductibleAmount();
            objs[9] = refundCheckDO.getOperatorName();
            objs[10] = DateUtil.formatDateTime(refundCheckDO.getCreateDate());
            objs[11] = refundCheckDO.getIsReverse() == 0 ? "否" : "是";
            dataList.add(objs);
        }
        ExportUtil exportUtil = new ExportUtil(title, rowsName, dataList);
        exportUtil.export(response);
    }
}
